﻿Imports System.Management.Automation
Imports System.Management.Automation.Host

Friend Class PSExecuteHostUserInterface
    Inherits PSHostUserInterface

    Private myRawUI As New PSExecuteHostRawUserInterface
    Private myStringBuilder As New System.Text.StringBuilder
    Public Event DataGenerated As EventHandler(Of PSExecuteStreamData)

    Private Sub GenerateEvent(message As String, Optional source As PSExecuteStreamDataSource = PSExecuteStreamDataSource.Output)
        message = message.TrimEnd
        If message.Length > 0 Then
            RaiseEvent DataGenerated(Me, New PSExecuteStreamData(message, source))
        End If
    End Sub

    Public Overrides ReadOnly Property RawUI As PSHostRawUserInterface
        Get
            Return Me.myRawUI
        End Get
    End Property

    Public Overloads Overrides Sub Write(value As String)
        If value.Length > 0 Then Me.myStringBuilder.Append(value)
    End Sub

    Public Overloads Overrides Sub Write(foregroundColor As ConsoleColor, backgroundColor As ConsoleColor, value As String)
        If value.Length > 0 Then Me.myStringBuilder.Append(value)
    End Sub

    Public Overloads Overrides Sub WriteLine(value As String)
        Me.myStringBuilder.AppendLine(value)
        Dim message As String = Me.myStringBuilder.ToString
        Me.myStringBuilder.Clear()
        GenerateEvent(message, PSExecuteStreamDataSource.Output)
    End Sub

    Public Overrides Sub WriteLine()
        Dim message As String = Me.myStringBuilder.ToString
        Me.myStringBuilder.Clear()
        GenerateEvent(message, PSExecuteStreamDataSource.Output)
    End Sub

    Public Overrides Sub WriteLine(foregroundColor As ConsoleColor, backgroundColor As ConsoleColor, value As String)
        Call Me.WriteLine(value)
    End Sub

    Public Overrides Sub WriteDebugLine(message As String)
        GenerateEvent(message, PSExecuteStreamDataSource.Debug)
    End Sub

    Public Overrides Sub WriteErrorLine(value As String)
        GenerateEvent(value, PSExecuteStreamDataSource.Error)
    End Sub

    Public Overrides Sub WriteProgress(sourceId As Long, record As ProgressRecord)
        GenerateEvent(String.Format("ACTIVITY: {0}. OPERATION: {1}. STATUS: {2}.", record.Activity, record.CurrentOperation, record.StatusDescription), PSExecuteStreamDataSource.Progress)
    End Sub

    Public Overrides Sub WriteVerboseLine(message As String)
        GenerateEvent(message, PSExecuteStreamDataSource.Verbose)
    End Sub

    Public Overrides Sub WriteWarningLine(message As String)
        GenerateEvent(message, PSExecuteStreamDataSource.Warning)
    End Sub

#Region "Not Implemented"
    Public Overrides Function Prompt(caption As String, message As String, descriptions As ObjectModel.Collection(Of FieldDescription)) As Dictionary(Of String, PSObject)
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function
    Public Overrides Function PromptForChoice(caption As String, message As String, choices As ObjectModel.Collection(Of ChoiceDescription), defaultChoice As Integer) As Integer
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function
    Public Overloads Overrides Function PromptForCredential(caption As String, message As String, userName As String, targetName As String) As PSCredential
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function
    Public Overloads Overrides Function PromptForCredential(caption As String, message As String, userName As String, targetName As String, allowedCredentialTypes As PSCredentialTypes, options As PSCredentialUIOptions) As PSCredential
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function
    Public Overrides Function ReadLine() As String
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function

    Public Overrides Function ReadLineAsSecureString() As System.Security.SecureString
        Throw New NotImplementedException("Not Implemented by PSExecuteHostUserInterface.")
    End Function
#End Region
End Class
